package com.etoak.controller;


import com.etoak.api.UploadService;
import com.etoak.common.core.vo.PageVO;
import com.etoak.common.core.vo.ResultVO;
import com.etoak.entity.Dishes;
import com.etoak.service.DishesService;
import com.etoak.vo.DishesVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/dishes")
public class DishesControoler {

    @Autowired
    UploadService uploadService;

    @Autowired
    DishesService dishesService;

    @PostMapping("/image")
    public ResultVO<String> uploadImage(MultipartFile file){
        return uploadService.uploadImage(file);
    }

    @PostMapping
    public ResultVO<Object> add(@Validated @RequestBody Dishes dishes){
        dishesService.add(dishes);
        return ResultVO.success();
    }

    @GetMapping("/list")
    public ResultVO<Object> list(
            @RequestParam(required = false,defaultValue = "1") int pageNumber,
            @RequestParam(required = false,defaultValue = "10") int pageSize,
            Dishes dishes){
        PageVO<DishesVO> pageVO = dishesService.getList(pageNumber, pageSize, dishes);
        return ResultVO.success(pageVO);
    }

    @PutMapping("/{id}")
    public ResultVO<Object> update(@PathVariable int id, @RequestBody Dishes dishes){
        dishesService.update(id,dishes);
        return ResultVO.success();
    }

    @DeleteMapping("/{id}")
    public ResultVO<Object> delete(@PathVariable int id){
        dishesService.delete(id);
        return ResultVO.success();
    }
}
